package xps.and.uudaijia.userclient.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.jude.utils.JUtils;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;


public class PushReceiver extends BroadcastReceiver {

    public static final String TAG = "PushReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Log.d(TAG, "onReceive - " + intent.getAction());
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            //收到自定义消息
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {     //自定义消息可以发送json数据

            System.out.println("收到了自定义消息。消息内容是：" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            // 自定义消息不会展示在通知栏，完全要开发者写代码去处理
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            System.out.println("收到了通知");
            // 在这里可以做些统计，或者做些其他工作(通知栏被点击)

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            System.out.println("用户点击打开了通知");

            try {
                String alert = intent.getStringExtra(JPushInterface.EXTRA_EXTRA);
                JUtils.Log("alert", alert);
                JSONObject jsonObject = new JSONObject(alert);
               /* if (jsonObject.getString("action").equals("pushorder")) {
                    driverReceive(jsonObject.getString("order_id"));
                    JUtils.Log(jsonObject.getString("order_id"));
                } else if (jsonObject.getString("action").equals("binddriver")) {
                    passReceive(jsonObject.getString("order_id"));
                    JUtils.Log(jsonObject.getString("order_id"));
                } else if (jsonObject.getString("action").equals("confirmaboard")) {
                    passReceive(jsonObject.getString("order_id"));
                    JUtils.Log(jsonObject.getString("order_id"));
                } else if (jsonObject.getString("action").equals("confirmarrive")) {
                    passReceive(jsonObject.getString("order_id"));
                    JUtils.Log(jsonObject.getString("order_id"));
                } else if (jsonObject.getString("action").equals("orderpay")) {
                    passPayActionReceive(jsonObject.getString("order_id"));
                    JUtils.Log(jsonObject.getString("order_id"));
                } else if (jsonObject.getString("action").equals("news")) {
                    ReceviveUserMsg(jsonObject.getString("news_id"));
                }*/

            } catch (JSONException e) {
                e.printStackTrace();
               /* Intent i = new Intent(XSApplication.getInstance(), MainActivity.class);//非指令消息都跳转到主页
                XSApplication.getInstance().startActivity(i);*/
            }

        } else if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            JUtils.Log("Received RegId:", JPushInterface.getRegistrationID(context));
        } else {
            Log.d(TAG, "Unhandled intent - " + intent.getAction());
        }
    }
}
